#include "newsongdialog.h"

NewSongDialog::NewSongDialog(QWidget *parent) :
    QDialog(parent)
{
    addressEdit = new QLineEdit(this);
    numberEdit = new QLineEdit(this);
    artistEdit = new QLineEdit(this);
    albumEdit = new QLineEdit(this);
    addressLabel = new QLabel("Youtube address:", this);
    artistLabel = new QLabel("Artist:", this);
    albumLabel= new QLabel("Album:", this);
    numberLabel = new QLabel("Song name:", this);
    okButton = new QPushButton("OK", this);

    QGridLayout* lay = new QGridLayout;
    lay->addWidget(addressLabel, 0, 0);
    lay->addWidget(addressEdit, 0, 1);
    lay->addWidget(numberLabel, 1, 0);
    lay->addWidget(numberEdit, 1, 1);
    lay->addWidget(artistLabel,2, 0);
    lay->addWidget(artistEdit, 2, 1);
    lay->addWidget(albumLabel, 3, 0);
    lay->addWidget(albumEdit, 3, 1);
    lay->addWidget(okButton, 4, 0);
    setLayout(lay);

    QObject::connect(okButton, SIGNAL(clicked()), this, SLOT(okClicked()));
    QObject::connect(addressEdit, SIGNAL(textChanged(QString)), this, SLOT(onUrlEntered(QString)));
}

void NewSongDialog::okClicked()
{
    if(!numberEdit->text().isEmpty())
        accept();
}

void NewSongDialog::onUrlEntered(QString url)
{
    QString artist = url.left(url.indexOf("-"));
    QString number = url.right(url.indexOf("-"));
    artistEdit->setText(artist);
    numberEdit->setText(number);
}
